home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / IFC_112 / netscape / application / KeyEvent.java < prev    next >
Encoding:
Text File  |  1999-05-28  |  11.0 KB  |  422 lines  |  [TEXT/CWIE]

  1. // KeyEvent.java
  2. // By Ned Etcode
  3. // Copyright 1996, 1997 Netscape Communications Corp.  All rights reserved.
  4.  
  5. package netscape.application;
  6.  
  7. import netscape.util.*;
  8.  
  9. import java.awt.Event;
  10.  
  11. /** Event subclass used for all key up and key down events.
  12.   * @note 1.0 added additional special keys for keyboard UI
  13.   * @note 1.1.1 added support for extended key events
  14.   */
  15.  
  16. public class KeyEvent extends netscape.application.Event {
  17.     /** The key that was pressed or released. */
  18.     public int                  key;
  19.  
  20.     /** The modifier keys that the user held down when the KeyEvent was
  21.       * generated.
  22.       */
  23.     public int                  modifiers;
  24.  
  25.     /** Key "down" event. */
  26.     public final static int     KEY_DOWN = -11;
  27.     /** Key "up" event. */
  28.     public final static int     KEY_UP = -12;
  29.  
  30.     /** Modifiers **/
  31.  
  32.     /** No modifiers
  33.       *
  34.       */
  35.     public static final int     NO_MODIFIERS_MASK = 0;
  36.  
  37.     /** The Alternate key modifier bitmask. */
  38.     public static final int     ALT_MASK = java.awt.Event.ALT_MASK;
  39.  
  40.     /** The Control key modifier bitmask. */
  41.     public static final int     CONTROL_MASK = java.awt.Event.CTRL_MASK;
  42.  
  43.     /** The Shift key modifier bitmask. */
  44.     public static final int     SHIFT_MASK = java.awt.Event.SHIFT_MASK;
  45.  
  46.     /** The Meta key modifier bitmask. */
  47.     public static final int     META_MASK = java.awt.Event.META_MASK;
  48.  
  49.     /** Special keys **/
  50.  
  51.     /** Return key **/
  52.     public final static int            RETURN_KEY = 10;
  53.  
  54.     /** Backspace key
  55.       *
  56.       */
  57.     public final static int            BACKSPACE_KEY = 8;
  58.  
  59.     /** Delete key
  60.       *
  61.       */
  62.     public final static int            DELETE_KEY = 127;
  63.  
  64.     /** Escape key
  65.       *
  66.       */
  67.     public final static int            ESCAPE_KEY = 27;
  68.  
  69.     /** Tab key
  70.       *
  71.       */
  72.     public final static int            TAB_KEY = 9;
  73.  
  74.     /** Up arrow key
  75.       *
  76.       */
  77.     public final static int            UP_ARROW_KEY = java.awt.Event.UP;
  78.  
  79.     /** Down arrow key
  80.       *
  81.       */
  82.     public final static int            DOWN_ARROW_KEY = java.awt.Event.DOWN;
  83.  
  84.     /** Left arrow key
  85.       *
  86.       */
  87.     public final static int            LEFT_ARROW_KEY = java.awt.Event.LEFT;
  88.  
  89.     /** Right arrow key
  90.       *
  91.       */
  92.     public final static int            RIGHT_ARROW_KEY = java.awt.Event.RIGHT;
  93.  
  94.     /** Home key
  95.       *
  96.       */
  97.     public final static int            HOME_KEY = java.awt.Event.HOME;
  98.  
  99.     /** End key
  100.       *
  101.       */
  102.     public final static int            END_KEY = java.awt.Event.END;
  103.  
  104.     /** Page up key
  105.       *
  106.       */
  107.     public final static int            PAGE_UP_KEY = java.awt.Event.PGUP;
  108.  
  109.     /** Page down key
  110.       *
  111.       */
  112.     public final static int            PAGE_DOWN_KEY = java.awt.Event.PGDN;
  113.  
  114.     /** F1 Key
  115.       *
  116.       */
  117.     public final static int            F1_KEY = java.awt.Event.F1;
  118.  
  119.     /** F2 Key
  120.       *
  121.       */
  122.     public final static int            F2_KEY = java.awt.Event.F2;
  123.  
  124.     /** F3 Key
  125.       *
  126.       */
  127.     public final static int            F3_KEY = java.awt.Event.F3;
  128.  
  129.     /** F4 Key
  130.       *
  131.       */
  132.     public final static int            F4_KEY = java.awt.Event.F4;
  133.  
  134.     /** F5 Key
  135.       *
  136.       */
  137.     public final static int            F5_KEY = java.awt.Event.F5;
  138.  
  139.     /** F6 Key
  140.       *
  141.       */
  142.     public final static int            F6_KEY = java.awt.Event.F6;
  143.  
  144.     /** F7 Key
  145.       *
  146.       */
  147.     public final static int            F7_KEY = java.awt.Event.F7;
  148.  
  149.     /** F8 Key
  150.       *
  151.       */
  152.     public final static int            F8_KEY = java.awt.Event.F8;
  153.  
  154.     /** F9 Key
  155.       *
  156.       */
  157.     public final static int            F9_KEY = java.awt.Event.F9;
  158.  
  159.     /** F10 Key
  160.       *
  161.       */
  162.     public final static int            F10_KEY = java.awt.Event.F10;
  163.  
  164.     /** F11 Key
  165.       *
  166.       */
  167.     public final static int            F11_KEY = java.awt.Event.F11;
  168.  
  169.     /** F12 Key
  170.       *
  171.       */
  172.     public final static int            F12_KEY = java.awt.Event.F12;
  173.  
  174.  
  175.     /** Constructs a KeyEvent.
  176.       */
  177.     public KeyEvent() {
  178.         super();
  179.     }
  180.  
  181.      /** Constructs a KeyEvent to hold key press information for the
  182.        * specified key.  <b>modifiers</b> is the bitmask representing the
  183.        * modifier keys held down during the key press.  <b>down</b> specifies
  184.        * whether the event represents a key up or key down event.
  185.        */
  186.     public KeyEvent(long timeStamp, int key, int modifiers, boolean down) {
  187.         this();
  188.  
  189.         this.timeStamp = timeStamp;
  190.         if (down) {
  191.             type = KEY_DOWN;
  192.         } else {
  193.             type = KEY_UP;
  194.         }
  195.         this.key = key;
  196.         this.modifiers = modifiers;
  197.     }
  198.  
  199.     /** Return whether this event is an extended key event. Extended
  200.       * key events are available with 1.1 virtual machine and handles
  201.       * Java virtual keys and unicode characters.
  202.       * To start receiving extended key events, your application object
  203.       * instance should override handleExtendedKeyEvent and return true.
  204.       */
  205.     public boolean isExtendedKeyEvent() {
  206.         return false;
  207.     }
  208.  
  209.     /** Return the virtual key associated with the event.
  210.       * This value is defined only if isExtendedKeyEvent() returns true
  211.       * To start receiving extended key events, your application object
  212.       * instance should override handleExtendedKeyEvent and return true.
  213.       */
  214.     public int keyCode() {
  215.       return 0;
  216.     }
  217.  
  218.     /** Return the unicode character associated with the event.
  219.       * This value is defined only if isExtendedKeyEvent() returns true
  220.       * To start receiving extended key events, your application object
  221.       * instance should override handleExtendedKeyEvent and return true.
  222.       */
  223.     public char keyChar() {
  224.       return (char)0;
  225.     }
  226.  
  227.     /** Returns <b>true</b> if the Shift key was held down during the key
  228.       * event.
  229.       */
  230.     public boolean isShiftKeyDown() {
  231.         return (modifiers & SHIFT_MASK) != 0;
  232.     }
  233.  
  234.     /** Returns <b>true</b> if the Control key was held down during the key
  235.       * event.
  236.       */
  237.     public boolean isControlKeyDown() {
  238.         return (modifiers & CONTROL_MASK) != 0;
  239.     }
  240.  
  241.     /** Returns <b>true</b> if the Meta key was held down during the key event.
  242.       */
  243.     public boolean isMetaKeyDown() {
  244.         return (modifiers & META_MASK) != 0;
  245.     }
  246.  
  247.     /** Returns <b>true</b> if the Alt key was held down during the key event.
  248.       */
  249.     public boolean isAltKeyDown() {
  250.         return (modifiers & ALT_MASK) != 0;
  251.     }
  252.  
  253.     /** Returns <b>true</b> if the KeyEvent represents the Return key.
  254.       */
  255.     public boolean isReturnKey() {
  256.         return key == RETURN_KEY;
  257.     }
  258.  
  259.     /** Returns <b>true</b> if the KeyEvent represents the Backspace key.
  260.       */
  261.     public boolean isBackspaceKey() {
  262.         return key == BACKSPACE_KEY;
  263.     }
  264.  
  265.     /** Returns <b>true</b> if the KeyEvent represents the Delete key.  In
  266.       * general, the key used to delete characters is the "Backspace" key in
  267.       * the upper-right corner of the keyboard (the Mac calls it "Delete").
  268.       * @see #isBackspaceKey
  269.       */
  270.     public boolean isDeleteKey() {
  271.         return key == DELETE_KEY;
  272.     }
  273.  
  274.     /** Returns <b>true</b> if the KeyEvent represents the Escape key.
  275.       */
  276.     public boolean isEscapeKey() {
  277.         return key == ESCAPE_KEY;
  278.     }
  279.  
  280.     /** Returns <b>true</b> if the KeyEvent represents the Tab key.
  281.       */
  282.     public boolean isTabKey() {
  283.         return (key == TAB_KEY && !isShiftKeyDown());
  284.     }
  285.  
  286.     /** Returns <b>true</b> if the KeyEvent represents the BackTab
  287.       * (Shift + Tab) key.
  288.       */
  289.     public boolean isBackTabKey() {
  290.         return (key == TAB_KEY && isShiftKeyDown());
  291.     }
  292.  
  293.     /** Returns <b>true</b> if the KeyEvent represents the Up Arrow key.
  294.       */
  295.     public boolean isUpArrowKey() {
  296.         return key == UP_ARROW_KEY;
  297.     }
  298.  
  299.     /** Returns <b>true</b> if the KeyEvent represents the Down Arrow key.
  300.       */
  301.     public boolean isDownArrowKey() {
  302.         return key == DOWN_ARROW_KEY;
  303.     }
  304.  
  305.     /** Returns <b>true</b> if the KeyEvent represents the Left Arrow key.
  306.       */
  307.     public boolean isLeftArrowKey() {
  308.         return key == LEFT_ARROW_KEY;
  309.     }
  310.  
  311.     /** Returns <b>true</b> if the KeyEvent represents the Right Arrow key.
  312.       */
  313.     public boolean isRightArrowKey() {
  314.         return key == RIGHT_ARROW_KEY;
  315.     }
  316.  
  317.     /** Returns <b>true</b> if the KeyEvent represents the Arrow key.
  318.       */
  319.     public boolean isArrowKey() {
  320.         return (key == UP_ARROW_KEY) || (key == DOWN_ARROW_KEY) ||
  321.                (key == LEFT_ARROW_KEY) || (key == RIGHT_ARROW_KEY);
  322.     }
  323.  
  324.     /** Returns <b>true</b> if the KeyEvent represents the Home key.
  325.       */
  326.     public boolean isHomeKey() {
  327.         return key == HOME_KEY;
  328.     }
  329.  
  330.     /** Returns <b>true</b> if the KeyEvent represents the End key.
  331.       */
  332.     public boolean isEndKey() {
  333.         return key == END_KEY;
  334.     }
  335.  
  336.     /** Returns <b>true</b> if the KeyEvent represents the Page Up key.
  337.       */
  338.     public boolean isPageUpKey() {
  339.         return key == PAGE_UP_KEY;
  340.     }
  341.  
  342.     /** Returns <b>true</b> if the KeyEvent represents the Page Down key.
  343.       */
  344.     public boolean isPageDownKey() {
  345.         return key == PAGE_DOWN_KEY;
  346.     }
  347.  
  348.     /** Returns the function key number or <b>0</b> if the KeyEvent does not
  349.       * represent a function key.
  350.      */
  351.     public int isFunctionKey() {
  352.         if (key == F1_KEY) {
  353.             return 1;
  354.         } else if (key == F2_KEY) {
  355.             return 2;
  356.         } else if (key == F3_KEY) {
  357.             return 3;
  358.         } else if (key == F4_KEY) {
  359.             return 4;
  360.         } else if (key == F5_KEY) {
  361.             return 5;
  362.         } else if (key == F6_KEY) {
  363.             return 6;
  364.         } else if (key == F7_KEY) {
  365.             return 7;
  366.         } else if (key == F8_KEY) {
  367.             return 8;
  368.         } else if (key == F9_KEY) {
  369.             return 9;
  370.         } else if (key == F10_KEY) {
  371.             return 10;
  372.         } else if (key == F11_KEY) {
  373.             return 11;
  374.         } else if (key == F12_KEY) {
  375.             return 12;
  376.         }
  377.  
  378.         return 0;
  379.     }
  380.  
  381.     /** Returns <b>true</b> if the KeyEvent represents a printable ASCII
  382.       * character.
  383.       */
  384.     public boolean isPrintableKey() {
  385.         return !((key < ' ') || isArrowKey() || isHomeKey() ||
  386.                  isEndKey() || (isFunctionKey() != 0) || isPageUpKey() ||
  387.                  isPageDownKey());
  388.     }
  389.  
  390.     /** Sets the RootView associated with the KeyEvent. */
  391.     public void setRootView(RootView rootView) {
  392.         processor = rootView;
  393.     }
  394.  
  395.     /** Returns the RootView associated with the KeyEvent.
  396.       * @see #setRootView
  397.       */
  398.     public RootView rootView() {
  399.         return (RootView)processor;
  400.     }
  401.  
  402.     /** Returns the KeyEvent's String representation.
  403.       */
  404.     public String toString() {
  405.         String          typeString;
  406.  
  407.         if (type == KEY_DOWN) {
  408.             typeString = "KeyDown";
  409.         } else {
  410.             typeString = "KeyUp";
  411.         }
  412.  
  413.         if (key < ' ') {
  414.             return typeString + ":\'\' (0x" +
  415.                    Integer.toString(key, 16) + ")\':" + modifiers;
  416.         }
  417.  
  418.         return typeString + ":\'" + (char)key + "\' (0x" +
  419.                Integer.toString(key, 16) + ")\':" + modifiers;
  420.     }
  421. }
  422.